package com.pri.chat.activity;

import android.view.View;

import com.google.gson.Gson;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.DetailListBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.adapter.DetailAdapter;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.utils.Des3Util;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxui.view.dialog.RxDialogRedSure;
import com.vondear.rxui.view.dialog.RxDialogSure;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 愿望金记录
 */
public class MoneyNoteListActivity extends BaseActivity {
    @BindView(R.id.rec_recharge_list)
    RecyclerView recRechargeList;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    private int page = 1;
    private int type = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cashnote_layout;
    }
    @Override
    protected void initView() {
        type = getIntent().getExtras().getInt("type");
        if(type == 0){
            setTitle("红包记录");
        }else if(type == 1){
            setTitle("愿望金记录");
        }else if(type == 2){
            setTitle("金币记录");
        }else if(type == 3){
            setTitle("会员充值记录");
        }else if(type == 4){
            setTitle("邀请提现记录");
        }
        initRv();
        initSrl();
    }
    private List<DetailListBean> mList = new ArrayList<>();
    private DetailAdapter mAdapter;
    private void initRv() {
        LinearLayoutManager linearLayoutManager =new LinearLayoutManager(this);
        recRechargeList.setLayoutManager(linearLayoutManager);
        recRechargeList.setNestedScrollingEnabled(false);
        mAdapter=new DetailAdapter(R.layout.item_details, mList);
        recRechargeList.setAdapter(mAdapter);
    }

    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            page++;
            initData();
        });
    }

    private void refresh() {
        page=1;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        srlAll.setEnableLoadmore(true);
        initData();
    }

    @Override
    protected void initData() {
        SubscriberOnNextListener<BaseBean<List<DetailListBean>>> onNextListener = baseBean -> {
            mList.addAll(baseBean.getData());
            mAdapter.notifyDataSetChanged();
            if(baseBean.getData().size()<10){
                srlAll.setEnableLoadmore(false);
            }
        };
        //0--红包；1--愿望基金；2--收益；3--超级会员
        Map<String,Object> params = new HashMap<>();//登录上传参数
        params.put("memberId",SharedHelper.readId(this));
        params.put("type",type);
        params.put("pageIndex",page+"");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().listCashNote(new ProgressSubscriber<>(onNextListener, this, true), requestBody);
    }
}
